﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RWLib
{
    public class TriggerStatement : StatementBase
    {

        private StatementBase action;
        private Formula condition;

        private List<StatementBase> triggeredActions = new List<StatementBase>();

        private bool running = false;

        public Formula Condition {
            get { return condition; }
        }

        public StatementBase Action {
            get {return action;}
        }

        public bool Running {
            get { return running; }
            set { running = value;  }
        }

        public TriggerStatement(string _name, Formula _condition, StatementBase _action) {
            name = _name;
            condition = _condition;
            action = _action;
        }

        override public List<List<Fluent>> Run(List<Fluent> fluents)
        {
            List<List<Fluent>> states = new List<List<Fluent>>();
            if (condition == null || condition.Eval(fluents))
            {                
                Tree._domain.Actions.Where(a => a.Name.Equals(action.Name)).ToList().ForEach(a => {
                    //if (!triggeredActions.Contains(a))
                    //{
                        //a.Run(fluents);
                        if (Tree._scenario.ActionsNames.ContainsKey(StartTime))
                        {
                            if (!Tree._scenario.ActionsNames[StartTime].Contains(a.Name))
                            {
                                a.StartTime = StartTime;
                                a.TriggeredBy = this;
                                running = true;
                                Tree._scenario.ActionsNames[a.StartTime].Add(a.Name);
                                triggeredActions.Add(a);
                            }
                        }
                        else
                        {
                            running = true;
                            a.StartTime = StartTime;
                            a.TriggeredBy = this;
                            Tree._scenario.ActionsNames.Add(a.StartTime, new List<string>() { a.Name });
                            triggeredActions.Add(a);
                        }
                    //}
                });    
            }
            else {
                running = false;
                return null;
            }
            return states;
        }

        public void release(StatementBase a) {
            triggeredActions.Remove(a);
            a.TriggeredBy = null;
            if (triggeredActions.Count() == 0) {
                Running = false;
            }
        }

        public void addToTriggered(StatementBase a) {
            triggeredActions.Add(a);
        }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(condition.ToString());
            sb.Append(" triggers ");
            sb.Append(action.Name);
            sb.Append(" Impossible:");
            sb.Append(ImpossibleCondition);
            return sb.ToString();
        }
    }
}